<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>以字符串的形式添加一个文件到 phar 档案</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="phar.addfile.html">« Phar::addFile</a></li>
      <li style="float: right;"><a href="phar.apiversion.html">Phar::apiVersion »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.phar.html">Phar</a></li>
    <li>以字符串的形式添加一个文件到 phar 档案</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="phar.addfromstring" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Phar::addFromString</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8, PECL phar &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">Phar::addFromString</span> &mdash; <span class="dc-title">以字符串的形式添加一个文件到 phar 档案</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-phar.addfromstring-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Phar::addFromString</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$localname</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$contents</code></span>): <span class="type"><span class="type void">void</span></span></div>

  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">此方法需要
将 <var class="filename">php.ini</var> 中的 <code class="literal">phar.readonly</code> 设为
<code class="literal">0</code> 以适合 <span class="classname"><a href="class.phar.html" class="classname">Phar</a></span>
对象.  否则, 将抛出<span class="classname"><a href="class.pharexception.html" class="classname">PharException</a></span>.</p></p></blockquote>

  <p class="para">
   通过这个方法，任何字符串都可以被添加到 phar 档案中。
   文件将会以 <code class="literal">localname</code> 为路径保存到档案中。
   这个方法与 <span class="function"><a href="ziparchive.addfromstring.html" class="function">ZipArchive::addFromString()</a></span> 类似。
  </p>

 </div>

 <div class="refsect1 parameters" id="refsect1-phar.addfromstring-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">localname</code></dt>

     <dd>

      <p class="para">
       文件保存到档案时的路径。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">contents</code></dt>

     <dd>

      <p class="para">
       要保存的文件内容。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-phar.addfromstring-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值，失败时会抛出异常。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-phar.addfromstring-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-716">
    <p><strong>示例 #1 一个 <span class="function"><strong>Phar::addFromString()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/phar.phar'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addFromString</span><span style="color: #007700">(</span><span style="color: #DD0000">'path/to/file.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my&nbsp;simple&nbsp;file'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'path/to/file.txt'</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getContent</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;to&nbsp;add&nbsp;contents&nbsp;from&nbsp;a&nbsp;stream&nbsp;handle&nbsp;for&nbsp;large&nbsp;files,&nbsp;use&nbsp;offsetSet()<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/path/to/hugefile.bin'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #DD0000">'largefile.bin'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;handle&nbsp;errors&nbsp;here<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-phar.addfromstring-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="phar.offsetset.html" class="function" rel="rdfs-seeAlso">Phar::offsetSet()</a> - Set the contents of an internal file to those of an external file</span></li>
    <li class="member"><span class="function"><a href="phardata.addfromstring.html" class="function" rel="rdfs-seeAlso">PharData::addFromString()</a> - Add a file from the filesystem to the tar/zip archive</span></li>
    <li class="member"><span class="function"><a href="phar.addfile.html" class="function" rel="rdfs-seeAlso">Phar::addFile()</a> - 将一个文件从文件系统添加到 phar 档案中</span></li>
    <li class="member"><span class="function"><a href="phar.addemptydir.html" class="function" rel="rdfs-seeAlso">Phar::addEmptyDir()</a> - 添加一个空目录到 phar 档案</span></li>
   </ul>
  </p>
 </div>



</div></div></div></body></html>